/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package group2assignone.dao;

import group2assignone.model.Customer;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Natalie
 */
public class Client implements ClientComms {

    private static final int DEFAULT_PORT = 5021;
    private static final String DEFAULT_HOST = "127.0.0.1";
    private BufferedReader reader;
    private PrintWriter writer;
    private Socket sock;
    private boolean successful = false;
    private Customer c;
    private static Client instance;

    private Client() {
    }

    public static Client getInstance() {
        if (instance == null) {
            instance = new Client();
        }
        return instance;
    }

    public boolean login(Customer c) {
        this.c = c;
        successful = false;

        setUpNetworking();
        registerCustomer();

        System.out.println("Exiting login function, returning successful");
        return successful;
    }

    private void registerCustomer() {
        String message = new String();
        writer.println(c.getUsername() + "," + c.getPassword());
        writer.flush();
        System.out.println("sending username,password");
        try {
            while ((message = reader.readLine()) == null) {
                System.out.println("waiting");
            }
            if (message.equals("Successful")) {
                successful = true;
                System.out.println("read: " + message);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
            successful = false;
        }

    }

    private void setUpNetworking() {
        try {
            sock = new Socket(DEFAULT_HOST, DEFAULT_PORT);
            InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
            reader = new BufferedReader(streamReader);
            writer = new PrintWriter(sock.getOutputStream());
            System.out.println("networking established");
        } catch (IOException ex) {
            ex.printStackTrace();
            successful = false;
        }
    }

    public void logout() {
        writer.println("Logging Out Now");
        writer.flush();
        writer.close();
        try {
            reader.close();
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("requesting logout");
    }

    /**
     * 
     * @return
     */
    public ArrayList<String> requestLoggedIn() {
        String s = new String();
        ArrayList<String> loggedIn = new ArrayList<String>();
        try {
            while ((s = reader.readLine()) == null) {
                System.out.println("waiting");
            }
            if (s.equals("Starting To Send")) {
                while (!(s = reader.readLine()).equals("End Of List")) {
                    loggedIn.add(s);
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
        return loggedIn;
    }
}
